/*
 * Copyright (C) 2004, Velocityme Partnership
 * ProjectActions.java
 *
 * Created on 06 October 2004, 08:17
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.entity.Project;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.NodeLocal;
import com.velocityme.session.ProjectLocal;
import com.velocityme.www.actionforms.nodeactionforms.ProjectActionForm;
import com.velocityme.www.utility.NodeUtil;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class ProjectActions extends ActivityActions {

    /** Creates a new instance of ProjectActions */
    public ProjectActions() {
    }

    @Override
    public ActionForward show(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Project project = lookupProjectBean().findProject(key, nodeId);
        session.setAttribute("project", project);
        session.setAttribute("projectHistory", lookupNodeBean().getHistory(key, project));
        return mapping.findForward("ACTION_DISPLAY");
    }

    @Override
    public ActionForward showCreate(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        configureCreate(session, key);
        session.setAttribute("personPairs", NodeUtil.getAllPersonPairs(key));
        ProjectActionForm projectAF = new ProjectActionForm();
        projectAF.setOwnerId(key.getPerson().getId());
        projectAF.setInterested(true);
        session.setAttribute("projectAF", projectAF);
        Project project = new Project();
        project.setParentNode(lookupNodeBean().findNode(key, nodeId));
        session.setAttribute("project", project);
        return mapping.findForward("ACTION_EDIT");
    }

    @Override
    public ActionForward showEdit(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Project project = lookupProjectBean().findProject(key, nodeId);
        session.setAttribute("project", project);
        configureEdit(session, key, project);
        ProjectActionForm projectAF = new ProjectActionForm();
        projectAF.setOwnerId(project.getOwner().getId());
        projectAF.setSponsorId(project.getSponsor().getId());
        projectAF.setPrimaryContactId(project.getPrimaryContact().getId());
        projectAF.setTaskTypeId(project.getTaskStateMachine().getId());
        projectAF.setPercentageComplete(project.getPercentageComplete());
        projectAF.setDurationTotalMins(project.getExpectedDuration_min());
        Calendar date = Calendar.getInstance();
        date.setTime(project.getStartDate());
        session.setAttribute("projectAF", projectAF);
        return mapping.findForward("ACTION_EDIT");
    }

    @Override
    public ActionForward edit(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Long nodeId;
        if (request.getParameter("btnSubmit").equals("OK")) {
            Project project = (Project)session.getAttribute("project");
            ((ProjectActionForm)form).extractProject(key, project);
            if (project.getId() != null) {
                lookupProjectBean().editProject(key, project);
            }
            else {
                project = lookupProjectBean().createProject(key, project, ((ProjectActionForm)form).isInterested());
            }
            nodeId = project.getId();
        }
        // Cancel simply displays the original node again
        else {
            nodeId = (Long)session.getAttribute("nodeId");
        }
        request.setAttribute("nodeId", nodeId);
        return mapping.findForward("ACTION_SUCCESS");
    }

    private ProjectLocal lookupProjectBean() {
        try {
            Context c = new InitialContext();
            return (ProjectLocal) c.lookup("java:comp/env/ProjectBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private NodeLocal lookupNodeBean() {
        try {
            Context c = new InitialContext();
            return (NodeLocal) c.lookup("java:comp/env/NodeBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
